package dao.impl;

import com.google.common.collect.ImmutableMap;
import common.consts.Commons;
import common.consts.Consts;
import dao.WorkLocationDao;
import domain.WorkLocation;
import org.springframework.stereotype.Repository;
import wrapper.WorkLocationWithDistance;

import java.util.List;
import java.util.Map;

/**
 * Created by aceyin on 15/6/25.
 */
@Repository
public class WorkLocationDaoImpl extends BaseGenericDao<WorkLocation, Long> implements WorkLocationDao {
    @Override
    public WorkLocation getByRecruitmentId(long id) {
        Map<String, Object> params = ImmutableMap.<String, Object>builder().put("recruitment_id", id).build();
        return queryOne("get-by-recruitment-id", params);
    }

    @Override
    public List<WorkLocation> findByCondition(Map<String, Object> map) {
        return queryList("find-by-condition", map);
    }

    @Override
    public List<WorkLocationWithDistance> findByCityAndConditionWith0Distance(String city, Map<String, Object> condition, int page) {
        int offset = getOffset(page, Consts.DEFAULT_ROWS);
        condition.put("city", city);
        condition.put("recruitment_status", Commons.Status.NORMAL.name());
        condition.put("offset", offset);
        condition.put("limit", Consts.DEFAULT_ROWS);
        return queryList("find-locations-by-city", condition);
    }

    @Override
    public List<WorkLocationWithDistance> findByUserCoordinateAndCity(String city, Map<String, Object> condition, Double lng, Double lat, int page) {

        int offset = getOffset(page, Consts.DEFAULT_ROWS);
        condition.put("city", city);
        condition.put("longitude", lng);
        condition.put("latitude", lat);
        condition.put("recruitment_status", Commons.Status.NORMAL.name());
        condition.put("offset", offset);
        condition.put("limit", Consts.DEFAULT_ROWS);

        return queryList("find-by-user-coordinate-and-city", condition);
    }

    @Override
    public WorkLocationWithDistance getByRecruitmentIdWithDistance(long recruitmentId, double longitude, double latitude) {
        Map<String, Object> params = newMapBuilder().put("recruitment_id", recruitmentId)
                .put("longitude", longitude)
                .put("latitude", latitude)
                .build();
        return queryOne("get-by-recruitment-id-with-distance", params);
    }
}
